﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using Kaiser.Modules.CMS.Repositories;
using Kaiser.Models;
using Kaiser.Modules.CMS.Models;
using Kaiser.Models.JsonModels;
using Kaiser.Modules.CMS.Models.InputModels;

namespace Kaiser.Modules.CMS.Services.Impl
{
    public class CMSManageService : ICMSManageService
    {
        private readonly ICMSRepositories _repository;

        public CMSManageService(ICMSRepositories repository)
        {
            _repository = repository;//
        }

        #region 分类管理


        public DataGrid<CategoryInput> GetCategoryAll(CategorySearch search, PagingInfo pageinfo)
        {
            try
            {
                return _repository.GetCategoryAll(search, pageinfo);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        public void InsertCategory(CategoryInput model)
        {
            try
            {
                _repository.InsertCategory(model);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void UpdateCategory(CategoryInput model)
        {
            try
            {
                _repository.UpdateCategory(model);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        public int DeleteCategory(string id)
        {
            try
            {
                return _repository.DeleteCategory(id);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int DeleteCategoryList(string ids)
        {
            try
            {
                return _repository.DeleteCategoryList(ids);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        public CategoryInput GetCategory(string Id)
        {
            try
            {
                return _repository.GetCategory(Id);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region 内容管理

        public DataGrid<ContentInput> QueryContent(CategorySearch search, PagingInfo pageinfo)
        {
            try
            {
                return _repository.QueryContent(search, pageinfo);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        /// <summary>
        /// 保持内容，同时新增关系表
        /// </summary>
        /// <param name="model"></param>
        /// <param name="CategoryId"></param>
        public void InsertContent(ContentInput model, IList<CategoryContentInput> cateModel)
        {
            try
            {
                _repository.InsertContent(model, cateModel);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        /// <summary>
        /// 修改内容，同时修改关系表
        /// </summary>
        /// <param name="model"></param>
        public void UpdateContent(ContentInput model, IList<CategoryContentInput> cateModel)
        {
            try
            {
 
                _repository.UpdateContent(model, cateModel);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 删除内容，同时删除关系
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public void DeleteContent(string id)
        {
            try
            {
                _repository.DeleteContentList(id);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        /// <summary>
        /// 删除多条内容
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public void DeleteContentList(string ids)
        {
            try
            {
                _repository.DeleteContentList(ids);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        #endregion


        public ContentInput GetContent(string id)
        {
            try
            {
                return _repository.GetContent(id);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        public IList<CategoryTree> CategoryTreeList(CategorySearch search, PagingInfo pageinfo)
        {
            try
            {
                DataGrid<CategoryInput> obj = _repository.GetCategoryAll(search, pageinfo);

                var list = new List<CategoryTree>();
                var children = obj.rows.Where(p => p.ParentId.ToString().Equals("0")).ToList();
                if (children.Count > 0)
                {
                    foreach (var categoryInput in children)
                    {
                        var node2 = new CategoryTree();
                        node2.id = categoryInput.Id.ToString(CultureInfo.InvariantCulture);
                        node2.name = categoryInput.CategoryName;
                        node2.ParentId = "0";
                        node2.Sequence = categoryInput.Sequence;
                        AddTreeNodes(node2, obj.rows, Convert.ToInt32(node2.id));

                        list.Add(node2);
                    }
                }
                return list;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void AddTreeNodes(CategoryTree perantNodes, IList<CategoryInput> List, int parendId)
        {
            var childrenList = List.Where(p => parendId != 0 && p.ParentId == parendId).ToList();
            if (childrenList.Count <= 0) return;

            perantNodes.children = new List<CategoryTree>();
            foreach (var categoryInput in childrenList)
            {
                var node3 = new CategoryTree();
                node3.id = categoryInput.Id.ToString(CultureInfo.InvariantCulture);
                node3.name = categoryInput.CategoryName;
                node3.ParentId = parendId.ToString();
                node3.Sequence = categoryInput.Sequence;
                AddTreeNodes(node3, List, Convert.ToInt32(node3.id));
                perantNodes.children.Add(node3);
            }
        }



    }
}
